


/**
 * 当前时间的问候语
 */
export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : (hour <= 11 ? '上午好' : (hour <= 13 ? '中午好' : (hour < 20 ? '下午好' : '晚上好')))
}


/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  let event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

/**
 * 循环过滤对象中为空的属性
 * @param obj
 * @returns {*}
 */
export function filterObj(obj) {
  if (!(typeof obj == 'object')) {
    return;
  }

  for ( var key in obj) {
    if(obj[key] == null || obj[key] == undefined || obj[key] === ''){
      delete obj[key];
    }else if(typeof obj[key] == 'object'){
        filterObj(obj[key]);
    }
  }
  return obj;
}


/**
 * 深度克隆对象、数组
 * @param obj 被克隆的对象
 * @return 克隆后的对象
 */
export function cloneObject(obj) {
  return JSON.parse(JSON.stringify(obj))
}

/**
 * 随机生成数字
 *
 * 示例：生成长度为 12 的随机数：randomNumber(12)
 * 示例：生成 3~23 之间的随机数：randomNumber(3, 23)
 *
 * @param1 最小值 | 长度
 * @param2 最大值
 * @return int 生成后的数字
 */
export function randomNumber() {
  // 生成 最小值 到 最大值 区间的随机数
  const random = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1) + min)
  }
  if (arguments.length === 1) {
    let [length] = arguments
  // 生成指定长度的随机数字，首位一定不是 0
    let nums = [...Array(length).keys()].map((i) => (i > 0 ? random(0, 9) : random(1, 9)))
    return parseInt(nums.join(''))
  } else if (arguments.length >= 2) {
    let [min, max] = arguments
    return random(min, max)
  } else {
    return Number.NaN
  }
}

/**
 * 随机生成字符串
 * @param length 字符串的长度
 * @param chats 可选字符串区间（只会生成传入的字符串中的字符）
 * @return string 生成的字符串
 */
export function randomString(length, chats) {
  if (!length) length = 1
  if (!chats) chats = '0123456789qwertyuioplkjhgfdsazxcvbnm'
  let str = ''
  for (let i = 0; i < length; i++) {
    let num = randomNumber(0, chats.length - 1)
    str += chats[num]
  }
  return str
}

/**
 * 随机生成uuid
 * @return string 生成的uuid
 */
export function randomUUID() {
  let chats = '0123456789abcdef'
  return randomString(32, chats)
}

export function megreRow(r, record, indexItemSerial, column, createElement, dataSource,page,length){
  let index =0;
  for (let i = 0; i < dataSource.length; i++) {
    if(record == dataSource[i]){
      index = i;
      break;
    }
  }

  if(indexItemSerial > 0){
    if(dataSource[index-1][column.dataIndex]==r){
      return {
        attrs: {rowSpan:0},
      };
    }
  }
  if(dataSource.length>index+1){
    for (let i = index+1; i < dataSource.length ; i++) {
      if(dataSource[i][column.dataIndex] != r || page.pageSize-indexItemSerial == 1){
        let cell = {
          children:createElement('AkEllipsis', {props:{
              value:r,length:length||10
            }}),
          attrs: {rowSpan:i-index},
        }
        return cell
      }
    }
    let cell={
      children:createElement('AkEllipsis', {props:{
          value:r,length:length||10
        }}),
      attrs: {rowSpan:dataSource.length-index},
    }
    return cell
  }
  return {
    children:createElement('AkEllipsis', {props:{
        value:r,length:length||10
      }}),
    attrs: {rowSpan:1},
  }
}

/**
 * 下划线转驼峰
 * @param string
 * @returns {*}
 */
export function underLine2CamelCase(string){
  return string.replace( /_([a-z])/g, function( all, letter ) {
    return letter.toUpperCase();
  });
}

/**
 * 连接url,
 * @param url1
 * @param url2
 * @returns {string}
 */
export function concatUrl(url1="", url2=""){
  while(url1.charAt(url1.length-1) == '/'){
    url1 = url1.substring(0, url1.length-1);
  }
  while(url2.charAt(0) == '/'){
    url2 = url2.substring(1);
  }
  return url1 + '/' +url2;
}

/**
 * @description 绑定事件 on(element, event, handler)
 */
export const on = (function () {
  if (document.addEventListener) {
    return function (element, event, handler) {
      if (element && event && handler) {
        element.addEventListener(event, handler, false)
      }
    }
  } else {
    return function (element, event, handler) {
      if (element && event && handler) {
        element.attachEvent('on' + event, handler)
      }
    }
  }
})()

/**
 * @description 解绑事件 off(element, event, handler)
 */
export const off = (function () {
  if (document.removeEventListener) {
    return function (element, event, handler) {
      if (element && event) {
        element.removeEventListener(event, handler, false)
      }
    }
  } else {
    return function (element, event, handler) {
      if (element && event) {
        element.detachEvent('on' + event, handler)
      }
    }
  }
})()

/**
* @param {*} data   [后台数据]
* @param {*} key    [要合并的字段]
* @param {*} target [后台数据对应的index]
* @returns 合并的行数
* method of 获取合并的行数
*/
export function getRowSpanCount(data, key, target) {
 if (!Array.isArray(data)) return 1;
 data = data.map(_ => _[key]); // 只取出筛选项
 let preValue = data[0];
 const res = [[preValue]]; // 放进二维数组里
 let index = 0; // 二维数组下标
 for (let i = 1; i < data.length; i++) {
   if (data[i] === preValue) { // 相同放进二维数组
     res[index].push(data[i]);
   } else { // 不相同二维数组下标后移
     index += 1;
     res[index] = [];
     res[index].push(data[i]);
     preValue = data[i];
   }
 }
 const arr = [];
 res.forEach((_) => {
   const len = _.length;
   for (let i = 0; i < len; i++) {
     arr.push(i === 0 ? len : 0);
   }
 });
 return arr[target];
}


export function loadJs(src, loaded) {

  let elements = document.getElementsByTagName("script")||[];
  for (let i = 0; i < elements.length; i++) {
    let element = elements[i];
    if(element.src == src){
      if(typeof loaded === 'function'){
        loaded();
      }
      return;
    }
  }
  let element = document.createElement("script");
  element.src = src;
  if(typeof loaded === 'function'){
    element.onload = loaded;
  }

  element.type = "text/javascript";
  document.body.appendChild(element);

}
